<template>
	<view>
		<form class="p-1">
			<view class="flex flex-column border-bottom">
				<input v-model="dept.name" type="text" class="uni-input pl-4" placeholder="请输入部门名称"/>
			</view>
			<view class="flex flex-column border-bottom">
				<view class="flex align-center">
					<textarea :auto-height="true" class="uni-input pl-4" v-model="dept.remark" placeholder="请输入部门备注" />
				</view>
			</view>
			<button @click="save" class="m-2 rounded-45 font bg-blue text-white" style="height: 65rpx;line-height: 65rpx;">确认</button>
		</form>
	</view>
</template>
<script setup>
import { ref,getCurrentInstance } from 'vue';
import {onLoad} from '@dcloudio/uni-app';
import {check, json} from '@/common/js/ceuer.js'
import common from '../common/js/common';

const dept=ref({name:null,remark:''});

function save() { 
	if(!checkData()) return;
	uni.showLoading({mask:true,title:'正在保存数据...'});
	uni.request({
		url: common.baseUrl+'/admin/dept/addOrUpdate',
		method: 'POST',
		data: dept.value,
		success: res => {
			uni.showToast({icon:'none',title:res.data.msg});
			if(res.data.flag){
				//通知该监听执行
				uni.$emit('refresh_dept_list');
				uni.navigateBack();
			}
		},
		complete: () => {
			uni.hideLoading();
		}
	});
}

function checkData() {
	if (check.empty(dept.value.name)){
		uni.showToast({title:'部门名称不能为空',icon:'none'});
		return false;
	}
	return true;//检查没有错误
}

onLoad((res)=>{
	if(res.dept){
		dept.value=JSON.parse(decodeURIComponent(res.dept));
	}
});
</script>